home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / SAT 2.1.2 / HeartQuest sample ƒ / main.p < prev    next >
Encoding:
Text File  |  1994-06-12  |  12.0 KB  |  268 lines  |  [TEXT/PJMM]

  1. {================================================}
  2. {=============== HeartQuest main unit ================}
  3. {================================================}
  4.  
  5. { Example file for Ingemars Sprite Animation Toolkit. }
  6. { © Ingemar Ragnemalm 1992 }
  7. { See doc files for legal terms for using this code. }
  8.  
  9. { HeartQuest is a very simple game demonstrating how to use the Sprite Animation}
  10. { Toolkit. I originally wrote the game as my present to my wife Eva for Valentine's}
  11. { day 1992. You can still tell that this file once started as the Skel example in the}
  12. { TransSkel package by Paul DuBois and Owen Hartnett. }
  13.  
  14. { This "main" file is rather small, and holds very little game specific code.}
  15. { Its main concern is to initialize the various parts of the game, and to hold the}
  16. { file and edit menu handlers. }
  17.  
  18. program HeartQuest;
  19.  
  20.     uses
  21.         TransSkel, SAT, GameGlobals, GameWind, {sound,}
  22.         SoundConst, scores, CenterStuff, Preferences, AppleEvents;
  23.  
  24. {Variables for the main program}
  25.     var
  26.         keys: KeyMap;
  27.         zoomFlag: Boolean;
  28.         ignore: longint;                        {For UnloadScrap error}
  29.         gAppleEventsInitialized: Boolean;    {For initializing Apple Events when necessary}
  30.  
  31. { -------------------------------------------------------------------- }
  32. {                        Menu handling procedures                        }
  33. { -------------------------------------------------------------------- }
  34.  
  35. {    Handle selection of "About…" item from Apple menu}
  36.  
  37.     procedure DoAbout;
  38.         var
  39.             ignore: integer;
  40.     begin
  41.         ignore := DoAlert(43, aboutAlrt, nil);
  42.     end;
  43.  
  44. {    Process selection from File menu.}
  45.  
  46. {    HelpEnemies    Shows a help box. }
  47. {    Quit    Request a halt by calling SkelHalt().  This makes SkelMain}
  48. {            return.}
  49.  
  50.     procedure DoFileMenu (item: integer);
  51.         var
  52.             ignore: integer;
  53.     begin
  54.         case item of
  55.             helpenemies: 
  56.                 ignore := DoAlert(43, helpenemiesAlrt, nil);
  57.             quit: 
  58.                 begin
  59.                     if pauseFlag then
  60.                         DoGameOver;
  61.                     SkelWhoa;
  62.                 end;
  63.             otherwise
  64.                 ;
  65.         end;
  66.     end;
  67.  
  68.     procedure DoEditMenu;
  69.     begin
  70.     end;
  71.  
  72. {    Initialize menus.  Tell TransSkel to process the Apple menu}
  73. {    automatically, and associate the proper procedures with the}
  74. {    File and Edit menus.}
  75.  
  76.     procedure SetUpMenus;
  77.     begin
  78.         SkelApple(MyGetIndString(aboutStrID), @DoAbout); {string 1: About HeartQuest…}
  79.         fileMenu := GetMenu(fileMenuRes);
  80.         editMenu := GetMenu(editMenuRes);
  81.         GameMenu := GetMenu(GameMenuRes);
  82.         highMenu := GetMenu(highMenuRes);
  83.         dummy := SkelMenu(fileMenu, @DoFileMenu, nil, false);
  84.         dummy := SkelMenu(editMenu, @DoEditMenu, nil, false);
  85.         dummy := SkelMenu(GameMenu, @DoGameMenu, nil, false);
  86.         dummy := SkelMenu(highMenu, @DoHighMenu, nil, true);
  87.     end;
  88.  
  89. { Initialize settings resources. These are saved in the game file itself. This is elegant,}
  90. { but a bit "server-hostile". An alternative is to create a preference file in the system}
  91. { folder. }
  92.  
  93.     procedure InitSettings;
  94.     begin
  95.         UseResFile(gPrefFile); {set the resfile to the pref file, if any. If none, gPrefFile will be the app itself!}
  96.         features := featHnd(GetResource('Feat', 0));        { Load the settings }
  97.         if features = nil then                                { Settings doesn't exist; create new }
  98.             begin
  99.                 features := featHnd(NewHandle(Sizeof(featRec)));
  100.                 CheckNoMem(Ptr(features));
  101.                 features^^.sound := true;
  102.                 features^^.allowBG := false;
  103.                 features^^.player := MyGetIndString(anonymousStrID); {str 2: Anonymous}
  104.                 features^^.macho := false;
  105.                 AddResource(handle(features), 'Feat', 0, 'Settings');
  106.             end
  107.         else                                                    {Did exist - check the size!}
  108.             if GetHandleSize(Handle(features)) < sizeof(featHnd) then
  109.                 SetHandleSize(Handle(features), sizeof(featHnd));
  110.         UseResFile(gAppFile);
  111.  
  112. { Fix all checkmarks in the menus }
  113.         if features^^.sound then
  114.             begin
  115.                 features^^.sound := false;
  116.                 DoGameMenu(sound);
  117.             end
  118.         else
  119.             begin
  120.                 features^^.sound := true;
  121.                 DoGameMenu(sound);
  122.             end;
  123.         if features^^.macho then
  124.             begin
  125.                 features^^.macho := false;
  126.                 DoGameMenu(macho);
  127.             end
  128.         else
  129.             begin
  130.                 features^^.macho := true;
  131.                 DoGameMenu(macho);
  132.             end;
  133.         if features^^.PlotFast then
  134.             begin
  135.                 features^^.PlotFast := false;
  136.                 DoGameMenu(FastAnimation);
  137.             end
  138.         else
  139.             begin
  140.                 features^^.PlotFast := true;
  141.                 DoGameMenu(FastAnimation);
  142.             end;
  143.         if features^^.allowBG then
  144.             begin
  145.                 features^^.allowBG := false;
  146.                 DoGameMenu(allowBG);
  147.             end
  148.         else
  149.             begin
  150.                 features^^.allowBG := true;
  151.                 DoGameMenu(allowBG);
  152.             end;
  153.     end;
  154.  
  155.  
  156. { ******* MultiFinder and Apple events: ******* }
  157.  
  158. {MultiFinder events - suspend and reume - have been handled by HeartQuest since very early versions,}
  159. {since I want it to hide its window when switched out.}
  160. {AppleEvents are added, mostly because I wanted to learn about it. I learned one thing: Apple Events are}
  161. {tedious. I tried simplifying AppleEvent support by installing my handlers first after getting an Apple}
  162. {Event (getting rid of all checking for its existence - if it sends events to me, it exists) - but the interface}
  163. {files needed are horrible. To speed up compilation, I made a stripped down interface file, HQAE.p.}
  164. {All I really got by supporting Apple Events is that I can quit after getting the 'quit' Apple event.}
  165.  
  166. {Handle the required Apple events:}
  167. {DoOpenApp,DoOpenDoc,DoPrintDoc,DoQuitApp}
  168. {MyGotRequiredParams: From MSG demo my Mark Pilgrim, tells whether we have handled all we have to or not.}
  169.     function MyGotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  170.         var
  171.             returnedType: DescType;
  172.             actualSize: Size;
  173.     begin
  174.         if AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, returnedType, nil, 0, actualSize) = errAEDescNotFound then
  175.             MyGotRequiredParams := noErr
  176.         else
  177.             MyGotRequiredParams := errAEParamMissed;
  178.     end;
  179.     function DoOpenApp (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  180.     begin
  181. {What am I supposed to do here?}
  182.         DoOpenApp := MyGotRequiredParams(theAppleEvent);
  183.     end;
  184.     function DoOpenDoc (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  185.     begin
  186.         DoOpenDoc := errAEEventNotHandled; {We don't open any documents!}
  187.     end;
  188.     function DoPrintDoc (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  189.     begin
  190.         DoPrintDoc := errAEEventNotHandled; {We don't print any documents!}
  191.     end;
  192.     function DoQuitApp (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  193.     begin
  194.         SkelWhoa;            {If I'm told to quit, I'll quit.}
  195.         DoQuitApp := MyGotRequiredParams(theAppleEvent);
  196.     end;
  197.  
  198. {Init Apple events}
  199. {Perhaps I'm cheating, but I don't call this until I get the first Apple event.}
  200. {IMHO, that's the simplest way to support them without a lot of boring Gestalt checks.}
  201.     procedure AppleEventInit;
  202.         var
  203.             error: OSerr;
  204.     begin
  205.         if gAppleEventsInitialized then
  206.             exit(AppleEventInit);
  207.         gAppleEventsInitialized := true;
  208.         error := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @DoOpenApp, 0, false);
  209.         error := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @DoOpenDoc, 0, false);
  210.         error := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @DoPrintDoc, 0, false);
  211.         error := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @DoQuitApp, 0, false);
  212. {I ignore errors.}
  213.     end;
  214.  
  215.  
  216. {Event processing that TransSkel doesn't handle (in the version I use here):}
  217. {MultiFinder events: Hide gameWindow on suspend, so the user can get access to disk icons etc.}
  218. {Apple Events: Handle the required Apple events.}
  219.  
  220.     procedure DoSuspendResume (b: boolean);
  221.     begin
  222.         if b then
  223. {Resume event: show game window and set the sleep time to something fairly low}
  224.             begin
  225.                 ShowWindow(gSAT.wind);
  226.                 SelectWindow(gSAT.wind);
  227.                 SkelSetSleep(5);
  228.             end
  229.         else
  230. {Susped event: Hide the game window and set the sleep time to something high}
  231. {(Not that the sleep time matters when "can background" is false, but I put it in for demonstrating it.)}
  232.             begin
  233.                 HideWindow(gSAT.wind);
  234.                 SkelSetSleep(60);
  235.             end;
  236.     end;
  237.  
  238.     function DoEvt (e: eventRecord): boolean;
  239.     begin
  240.         if e.what = OSevt then
  241.             begin
  242.                 if BAND(BROTL(e.message, 8), $FF) = SuspendResumeMessage then
  243.                     DoSuspendResume(BAnd(e.message, 1) <> 0);
  244.                 DoEvt := true;
  245.             end
  246.         else if e.what = kHighLevelEvent then
  247.             begin
  248.                 if not gAppleEventsInitialized then {My little "cheat" into compatibility}
  249.                     AppleEventInit;
  250.                 if AEProcessAppleEvent(e) <> noErr then
  251.                     ;
  252.             end
  253.         else
  254.             DoEvt := false;
  255.     end; { DoEvt }
  256.  
  257.  
  258. {Do a quick approximation of how much memory we need.}
  259. {(This is rather unnecessary since SAT has all the checks we need. I put it in in the old days when}
  260. {SAT still had a few missing error checks - which I hope are all fixed now.)}
  261.     function GetMemoryDemand: Longint;
  262.         const
  263.             faceCount = 19;
  264.             spriteCount = 30; {We can get *lots* of sprites in this game!}
  265.         var
  266.             theWorld: SysEnvRec;
  267.             error: OSerr;
  268.             testDepth, testHeight, t